package com.epfresh.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.epfresh.R;
import com.epfresh.api.widget.tablayout.CommonTabLayout;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.fragment.*;
import com.epfresh.global.BaseActivity;

import java.util.ArrayList;

import com.epfresh.api.global.AppFragment;

public class BookPayActivity extends BaseActivity {
    private CommonTabLayout mTabLayout;
    private ViewPager mViewPager;
    private final int TAB_COUNT = 2;
    Context mContext;
    public static String id="";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }
    AppFragment fragments[] = new AppFragment[2];
    String timeValue = "";
    int timeFlag = 1;//1:year;2month;
    public void initView(){
        setContentView(R.layout.activity_book_pay);
        mContext = this;
        basetitle.setText("已付未付");
        timeValue = getIntent().getStringExtra("timeValue");
        if(timeValue.length()==4){
            timeFlag = 1;
        }else{
            timeFlag = 2;
        }
        baseback.setVisibility(View.VISIBLE);
        mTabLayout = (CommonTabLayout) findViewById(R.id.id_tablayout);
        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        setAdapter();
        mViewPager.setCurrentItem(getIntent().getIntExtra("type",0));//0未支付；1已支付
        mViewPager.setOffscreenPageLimit(1);
    }

    public void setAdapter(){
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                if(fragments[i]==null){
                    if(timeFlag==1)
                        fragments[i] = new BookPayYearFragment(i,timeValue);
                    else
                        fragments[i] = new BookPayMonthFragment(i,timeValue);

                    return fragments[i];
                }else {
                    return fragments[i];
                }
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "未支付";
                    case 1:
                        return "已支付";
                }
                return "Helloworld";
            }

            @Override
            public int getCount() {
                return TAB_COUNT;
            }
        });
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
                if(fragments[position]==null){
                    return;
                }else {
                    /*判空 以及if(fragments[position] instanceof BookPayYearFragment)*/
                    if(timeFlag==1) {
                        ((BookPayYearFragment) fragments[position]).intoFresh();
                    }
                    else {
                        ((BookPayMonthFragment) fragments[position]).intoFresh();
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        String[] mTitles = {"未支付", "已支付"};
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }

        mTabLayout.setTabData(mTabEntities);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }


}
